
 <!--  用户管理 -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb> 
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="uerInline" class="user-search">
      <el-form-item label="">
        <el-input size="small" v-model="uerInline.userName" placeholder="输入用户名"></el-input>
      </el-form-item>
       <el-form-item label="">
        <el-input size="small" v-model="uerInline.userMobile" placeholder="输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="userEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading" border element-loading-text="数据加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="userName" label="用户名" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="userRole" label="角色" width="180">
      </el-table-column>
      <el-table-column align="center" sortable prop="userMobile" label="手机号" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="userSex" label="性别" min-width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="userEmail" label="邮箱" min-width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="editTime" label="最后登录时间" min-width="120">
        <template slot-scope="scope">
          <div>{{scope.row.editTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" sortable prop="isLock" label="操作状态" min-width="50">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isLock=='N'?nshow:fshow" active-color="#13ce66" inactive-color="#ff4949" @change="editType(scope.$index, scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center"  label="操作" width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="userEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="delUser(scope.$index, scope.row)">删除</el-button>
          <el-button size="mini" type="primary" @click="resetpwd(scope.$index, scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pagination" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="userFormVisible" width="30%" @click='closeUser("edit")'>
      <el-form label-width="80px" ref="userForm" :model="userForm" :rules="rules">
        <el-form-item label="用户名" prop="userName">
          <el-input size="small" v-model="userForm.userName" auto-complete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="userRole">
          <el-input size="small" v-model="userForm.userRole" auto-complete="off" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="userMobile">
          <el-input size="small" v-model="userForm.userMobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮件" prop="userEmail">
          <el-input size="small" v-model="userForm.userEmail" placeholder="请输入邮箱地址"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="userSex">
          <el-radio v-model="userForm.userSex" label="男">男</el-radio>
          <el-radio v-model="userForm.userSex" label="女">女</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeUser("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('userForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import {
  userList,
  userSave,
  userDelete,
  userPwd,
  userExpireToken,
  userFlashCache,
  userLock,
  UserDeptTree,
  UserDeptSave,
  UserDeptdeptTree,
  UserChangeDept
} from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: '添加用户',
      userFormVisible: false, //控制编辑页面显示与隐藏
      // 编辑与添加
      userForm: {
        userId: '',
        userName: '',
        userRole: '',
        roleId: '',
        userMobile: '',
        userEmail: '',
        userSex: '',
        token: localStorage.getItem('logintoken')
      },
      // 参数
      unitparm: {
        userIds: '',
        deptId: '',
        deptName: ''
      },
      // 选择数据
      selectdata: [],
      // rules表单验证
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        userRole: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        roleId: [{ required: true, message: '请选择角色', trigger: 'blur' }],
        userMobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
            required: true,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        userEmail: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            required: true,
            message: '请输入正确的邮箱',
            trigger: 'blur'
          }
        ],
        userSex: [{ required: true, message: '请选择性别', trigger: 'blur' }]
      },
      // 删除用户
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      // 重置密码
      resetpsd: {
        userId: '',
        token: localStorage.getItem('logintoken')
      },
      // 请求数据参数
      uerInline: {
        page: 1,
        limit: 10,
        deptId: '',
        userName: '',
        userMobile: '',
        isLock: ''
      },
      //用户数据
      userData: [],
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: '',
      // 分页参数
      pagination: {
        currentPage: 1,
        pageSize:5,
        total:5
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },


  watch: {},
  created() {
    this.getdata(this.uerInline)
  },

  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true
      //测试环境使用数据
      let res = {
        code: 0,
        msg: null,
        count:7,
        data: [
          {
            addUser: '1',
            editUser: '1',
            addTime: null,
            editTime: "2024年04月20日",
            userId: 1,
            systemNo: 'pmd',
            userName: 'admin',
            userPassword: '21232f297a57a5a743894a0e4a801fc3',
            userRole: '系统管理员',
            userSex: '男',
            userMobile: '188-xxxx-xxxx',
            userEmail: 'admin@example.com',
            isLock: 'N',
            deptId: 1,
            deptName: 'xxxx',
            roleId: 1
          },
          {
            addUser: '2',
            editUser: '2',
            addTime: null,
            editTime: "2024年04月05日",
            userId: 2,
            systemNo: 'mc',
            userName: 'test',
            userPassword: 'd41d8cd98f00b204e9800998ecf8427e',
            userRole: '管理员',
            userSex: '女',
            userMobile: '159-xxxx-xxxx',
            userEmail: 'test@example.com',
            isLock: 'N',
            deptId: 2,
            deptName: 'xxxx',
            roleId: 2
          },
          {
            addUser: '3',
            editUser: '3',
            addTime: null,
            editTime: "2024年04月15日",
            userId: 3,
            systemNo: 'ec',
            userName: '梦境引导者',
            userPassword: '670b14728ad9902aecba32e2cfb72f03',
            userRole: '数字虚拟人专员',
            userSex: '男',
            userMobile: '188-xxxx-xxxx',
            userEmail: 'mingjing@example.com',
            isLock: 'N',
            deptId: 3,
            deptName: 'xxxx',
            roleId: 3
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526275128000,
            editTime: "2024年04月20日",
            userId: 4,
            systemNo: null,
            userName: '星语聆听者',
            userPassword: 'e4d909c290d0fb1ca068ffaddf22cbd0',
            userRole: '高级数字虚拟人',
            userSex: '男',
            userMobile: '178-xxxx-xxxx',
            userEmail: 'starlistener@example.com',
            isLock: 'N',
            deptId: 4,
            deptName: 'zxxxxx',
            roleId: 4
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526275128000,
            editTime: "2024年04月20日",
            userId: 5,
            systemNo: null,
            userName: '诗意对话者',
            userPassword: '03ac674216f3e15c761ee1a5e255f01f',
            userRole: '数字虚拟人主管',
            userSex: '女',
            userMobile: '135-xxxx-xxxx',
            userEmail: 'poeticchatter@example.com',
            isLock: 'N',
            deptId: 5,
            deptName: 'zxxxxx',
            roleId: 4
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526275128000,
            editTime: "2024年04月19日",
            userId: 6,
            systemNo: null,
            userName: '心声解读者',
            userPassword: '827ccb0eea8a706c4c34a16891f84e7b',
            userRole: '数字虚拟人顾问',
            userSex: '女',
            userMobile: '176-xxxx-xxxx',
            userEmail: 'voiceinterpreter@example.com',
            isLock: 'N',
            deptId: 6,
            deptName: 'zxxxxx',
            roleId: 4
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526275128000,
            editTime: "2024年04月20日",
            userId: 7,
            systemNo: null,
            userName: '时光倾听者',
            userPassword: 'a87ff679a2f3e71d9181a67b7542122c',
            userRole: '数字虚拟人代表',
            userSex: '男',
            userMobile: '132-xxxx-xxxx',
            userEmail: 'timelistener@example.com	',
            isLock: 'N',
            deptId: 7,
            deptName: 'zxxxxx',
            roleId: 4
          },


        ]
      }
      this.loading = false
      this.userData = res.data
      // 分页赋值
      this.pagination.currentPage = this.uerInline.page
      this.pagination.pageSize = this.uerInline.limit
      this.pagination.total = res.count
      // 测试环境  模拟数据结束  

      /***
       *  正式环境环境 调用接口
       */
      // 获取用户列表
      userList(parameter).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.userData = res.data
          // 分页赋值
          this.pagination.currentPage = this.uerInline.page
          this.pagination.pageSize = this.uerInline.limit
          this.pagination.total = res.count
        }
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.uerInline.page = parm.currentPage
      this.uerInline.limit = parm.pageSize
      this.getdata(this.uerInline)
    },
    //搜索事件
    search() {
      this.getdata(this.uerInline)
    },
    // 修改type
    editType: function(index, row) {
      this.loading = true
      let parm = {
        lock: '',
        userId: '',
        token: localStorage.getItem('logintoken')
      }
      parm.userId = row.userId
      let lock = row.isLock
      if (lock == 'N') {
        parm.lock = 'Y'
      } else {
        parm.lock = 'N'
      }
      // 修改状态
      userLock(parm).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.$message({
            type: 'success',
            message: '状态修改成功'
          })
          this.getdata(this.uerInline)
        }
      })
    },
    //显示编辑界面
    userEdit: function(index, row) {
      this.userFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改用户'
        this.userForm.userId = row.userId
        this.userForm.userName = row.userName
        this.userForm.userRole = row.userRole
        this.userForm.roleId = row.roleId
        this.userForm.userMobile = row.userMobile
        this.userForm.userEmail = row.userEmail
        this.userForm.userSex = row.userSex
      } else {
        this.title = '添加用户'
        this.userForm.userId = ''
        this.userForm.userName = ''
        this.userForm.userRole = ''
        this.userForm.roleId = ''
        this.userForm.userMobile = ''
        this.userForm.userEmail = ''
        this.userForm.userSex = ''
      }
    },
    // 编辑、添加提交方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          userSave(this.userForm)
            .then(res => {
              this.userFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.uerInline)
                this.$message({
                  type: 'success',
                  message: '用户保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.userFormVisible = false
              this.loading = false
              this.$message.error('用户保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    handleClick(data, checked, node) {
      if (checked) {
        this.$refs.tree.setCheckedNodes([])
        this.$refs.tree.setCheckedNodes([data])
        this.unitparm.deptId = data.id
        this.unitparm.deptName = data.name
        //交叉点击节点
      } else {
      }
    },

    closeUser(dialog) {
        this.userFormVisible = false
     
    },
    // 重置密码
    resetpwd(index, row) {
      this.resetpsd.userId = row.userId
      this.$confirm('确定要重置用户密码吗?', '重置密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          userPwd(this.resetpsd)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '重置用户密码成功！'
                })
                this.getdata(this.uerInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('重置用户密码失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消重置用户密码！'
          })
        })
    },

  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 